﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Tomahawk.Serialization;
using Tomahawk.Serialization.SerialBoost;

namespace Tomahawk.Resources.Particles
{
    public sealed class ParticleSettingsCollection : IDisposable
    {
        public delegate void OnDisposedDelegate(ParticleSettingsCollection collection);
        public event OnDisposedDelegate OnDispose;

        #region Serializable Properties
        [XmlElement("Collection")]
        public SerializableList<ParticleSettings> collection = new SerializableList<ParticleSettings>();

        private Dictionary<string, ParticleSettings> particleSettings = new Dictionary<string, ParticleSettings>(StringComparer.OrdinalIgnoreCase);

        [XmlIgnore]
        public Dictionary<string, ParticleSettings> ParticleSettings { get { return particleSettings; } }
        #endregion

        #region Serialization
        public void Serialize(string file)
        {
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer x = new XmlSerializer(typeof(ParticleSettingsCollection));
                x.Serialize(fs, this);
            }
        }

        public static ParticleSettingsCollection Deserialize(string file)
        {
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer x = new XmlSerializer(typeof(ParticleSettingsCollection));
                return (ParticleSettingsCollection)x.Deserialize(fs);
            }
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            OnDispose(this);
        }

        #endregion
    }
}
